package com.hanbang.gycatering.aboutme;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.hanbang.gangyu.R;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.bean.CommodityData;
import com.hanbang.gycatering.classify.ProductDetail;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.PagingHelp;
import com.hanbang.gycatering.utils.SystemTools;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class IntegralMall extends BaseActivity implements OnClickListener {
	private PullToRefreshListView listView = null;
	private MyAdapter adapter;
	private ArrayList<CommodityData> commodityListDatas = new ArrayList<CommodityData>();
	private int id = 53;
	private LinearLayout tab1, tab2;
	private PagingHelp pagingHelp = new PagingHelp();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		id = getIntent().getIntExtra("id", id);
		setContentView(R.layout.classifylist);
		initView();
		setTheClick();
		initListView();
		getProductList(1, 2);
		selectButton(1, 2);
		super.setBackArrows((ImageView) findViewById(R.id.top_bar_left_image));
	}
	private void getProductList(int type, int or) {
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.productlist + "&id=" + id
						+ "&PageIndex=" + pagingHelp.getPageindex()
						+ "&sortOrderBy=" + type + "&sortOrder=" + or,
				new MyRequestCallBack<String>(IntegralMall.this,
						getString(R.string.loadding)) {
					@Override
					public void onFailure(HttpException error, String msg) {
						super.onFailure(error, msg);
						listView.onRefreshComplete();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								pagingHelp.setRecordCount(json
										.getInt("recordCount"));
								pagingHelp.setPageCount(json
										.getInt("pageCount"));
								JSONArray info = json.getJSONArray("info");

								int i = 0;
								for (i = pagingHelp.getCurrentCount(); i < info
										.length(); i++) {
									CommodityData data = new CommodityData();
									JSONObject item = info.getJSONObject(i);
									data.setId(item.getInt("id"));
									data.setName(item.getString("name"));
									data.setPrice(item.getString("price"));
									data.setImg(item.getString("img"));
									commodityListDatas.add(data);
								}

								pagingHelp.isNextPaging(IntegralMall.this,
										info.length(), i);
							} else {
								pagingHelp.showNoData(IntegralMall.this);
							}
							adapter.notifyDataSetChanged();
						} catch (JSONException e) {
							e.printStackTrace();
						}
						listView.onRefreshComplete();
					}
				});
	}

	private void initListView() {
		listView.setMode(Mode.BOTH);
		listView.setOnItemClickListener(onItemClickListener);
		adapter = new MyAdapter();
		listView.setAdapter(adapter);
		listView.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				pagingHelp.clear();
				commodityListDatas.clear();
				if (tab1.isEnabled()) {
					// 现在处于tab2
					if ("1".equals(tab2.getChildAt(1).getTag())) {
						getProductList(2, 1);
					} else if ("2".equals(tab2.getChildAt(1).getTag())) {
						getProductList(2, 2);
					}
				} else {
					// 现在处于tab1
					selectButton(1, 2);
					getProductList(1, 2);
				}
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				if (tab1.isEnabled()) {
					// 现在处于tab2
					if ("1".equals(tab2.getChildAt(1).getTag())) {
						getProductList(2, 1);
					} else if ("2".equals(tab2.getChildAt(1).getTag())) {
						getProductList(2, 2);
					}
				} else {
					// 现在处于tab1
					selectButton(1, 2);
					getProductList(1, 2);
				}
			}
		});
		listView.setOnItemClickListener(onItemClickListener);
	}

	OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, final View view,
				final int position, long id) {
			Intent intent = new Intent(IntegralMall.this, ProductDetail.class);
			intent.putExtra("id", commodityListDatas.get(position - 1).getId());
			startActivity(intent);
		}
	};

	private void setTheClick() {
		// TODO Auto-generated method stub
		((ImageView) findViewById(R.id.top_bar_left_image))
				.setOnClickListener(this);

		tab1.setOnClickListener(this);
		tab2.setOnClickListener(this);
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) findViewById(R.id.top_bar_title)).setText("积分商城");
		listView = (PullToRefreshListView) findViewById(R.id.listview);
		tab1 = (LinearLayout) findViewById(R.id.tab1);
		tab2 = (LinearLayout) findViewById(R.id.tab2);
		tab2.getChildAt(1).setTag("1");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tab1:
			selectButton(1, 2);
			commodityListDatas.clear();
			pagingHelp.clear();
			getProductList(1, 2);
			break;
		case R.id.tab2:
			if ("1".equals(tab2.getChildAt(1).getTag())) {
				selectButton(2, 2);
				commodityListDatas.clear();
				pagingHelp.clear();
				getProductList(2, 2);
			} else if ("2".equals(tab2.getChildAt(1).getTag())) {
				selectButton(2, 1);
				commodityListDatas.clear();
				pagingHelp.clear();
				getProductList(2, 1);
			}

			break;

		default:
			break;
		}

	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return commodityListDatas.size();
		}

		@Override
		public Object getItem(int position) {
			return commodityListDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			CommodityData data = commodityListDatas.get(position);
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(
						R.layout.classify_list_list_item, null);
				holder.imageView = (ImageView) convertView
						.findViewById(R.id.image);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.price = (TextView) convertView.findViewById(R.id.price);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			bitmapUtils.display(holder.imageView,
					SystemTools.getHttpFileUrl(data.getImg()));
			holder.name.setText(data.getName());
			holder.price.setText(data.getPrice()+"积分");
			return convertView;
		}

		private class ViewHolder {
			ImageView imageView = null;
			TextView name = null;
			TextView price = null;
		}
	}

	/*
	 * 选择按钮 1：销量 2：价格 1:升序 2：降序
	 */
	private void selectButton(int i, int or) {
		if (i == 1) {
			tab1.setEnabled(false);
			if (or == 1) {
				((ImageView) tab1.getChildAt(1))
						.setImageResource(R.drawable.up_arrows);
			} else if (or == 2) {
				((ImageView) tab1.getChildAt(1))
						.setImageResource(R.drawable.down_arrows);
			}
			((TextView) tab1.getChildAt(0)).setTextColor(getResources()
					.getColor(R.color.red));
			((ImageView) tab1.getChildAt(1)).setColorFilter(getResources()
					.getColor(R.color.red));

			((TextView) tab2.getChildAt(0)).setTextColor(getResources()
					.getColor(R.color.black));
			((ImageView) tab2.getChildAt(1)).setColorFilter(getResources()
					.getColor(R.color.black));

		} else if (i == 2) {
			tab1.setEnabled(true);
			if (or == 1) {
				((ImageView) tab2.getChildAt(1))
						.setImageResource(R.drawable.up_arrows);
				tab2.getChildAt(1).setTag("1");
			} else if (or == 2) {
				((ImageView) tab2.getChildAt(1))
						.setImageResource(R.drawable.down_arrows);
				tab2.getChildAt(1).setTag("2");
			}
			((TextView) tab2.getChildAt(0)).setTextColor(getResources()
					.getColor(R.color.red));
			((ImageView) tab2.getChildAt(1)).setColorFilter(getResources()
					.getColor(R.color.red));

			((TextView) tab1.getChildAt(0)).setTextColor(getResources()
					.getColor(R.color.black));
			((ImageView) tab1.getChildAt(1)).setColorFilter(getResources()
					.getColor(R.color.black));
		}
	}
}

